import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ParseIntPipe,
  Query,
} from '@nestjs/common';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';

@ApiTags('角色管理')
@Controller('role')
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @ApiOperation({ summary: '新增角色' })
  @Post()
  create(@Body() createRoleDto: CreateRoleDto) {
    return this.roleService.create(createRoleDto);
  }

  @ApiOperation({ summary: '查询所有角色列表' })
  @Get()
  findAll(
    @Query('page', new ParseIntPipe()) page = 1,
    @Query('pageSize', new ParseIntPipe()) pageSize = 10,
  ) {
    return this.roleService.findAll({ page, pageSize });
  }


  @ApiOperation({ summary: '分页查询角色列表' })
  @ApiQuery({
    name: 'page',
    type: Number,
    description: '当前页码',
    required: true,
    example: 1,
  })
  @ApiQuery({
    name: 'pageSize',
    type: Number,
    description: '分页大小',
    required: true,
    example: 10,
  })
  @ApiQuery({
    name: 'name',
    type: String,
    description: '角色名称',
    required: false,
    example: '管理员',
  })
  @ApiQuery({
    name: 'code',
    type: String,
    description: '角色编码',
    required: false,
    example: 'code',
  })
  @Get('page')
  pageSearch(
    @Query('page', new ParseIntPipe()) page = 1,
    @Query('pageSize', new ParseIntPipe()) pageSize = 10,
    @Query('name') name?: string,
    @Query('code') code?: string,
  ) {
    return this.roleService.pageSearch({
      page,
      pageSize,
      name,
      code,
    });
  }

  @ApiOperation({ summary: '查询角色详情' })
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.roleService.findOne(id);
  }

  @ApiOperation({ summary: '更新角色信息' })
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto) {
    return this.roleService.update(id, updateRoleDto);
  }

  @ApiOperation({ summary: '删除角色' })
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.roleService.remove(id);
  }
}
